/*
 * @Description: 新建检测方案 - 基本信息
 * @Author: Rfan
 * @Date: 2022-10-10 09:27:29
 * @LastEditTime: 2022-10-21 18:12:06
 */

import { forwardRef, Ref, useEffect, useImperativeHandle, useState } from 'react';
import { Form, Input } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { useArchivesDetectionContext } from '../../../../contexts/ArchivesDetectionContext';
import { EnumDialogType, useAddPlanDialogContext } from '../contexts/AddPlanDialogContext';
import styles from '../styles/main.module.scss';
import { IAddPlanParams } from '@/api/standardizedDetection/detectionPlan';

const cx = classnames.bind(styles);

const BaseInfo = forwardRef((props: any, ref: Ref<unknown>) => {
  const { selectAT, addDetectionPlanFunc, editDetectionPlanFunc } = useArchivesDetectionContext();
  const {
    visible,
    dialogType,
    currentObjType,
    currentPlanInfo,
    currentStep,
    setCurrentStep,
    setCurrentPlanInfo,
    getDetectionRules
  } = useAddPlanDialogContext();
  const [form] = Form.useForm();

  const [editFlag, setEditFlag] = useState(false);

  useImperativeHandle(ref, () => ({
    submit: () => {
      if (!currentPlanInfo?.id || (currentPlanInfo?.id && editFlag)) {
        form.submit();
      } else if (currentPlanInfo?.id) {
        getRulesNext();
      } else {
        setCurrentStep(currentStep + 1);
      }
    }
  }));

  useEffect(() => {
    if (visible) {
      form.setFieldsValue(currentPlanInfo);
    }
  }, [visible]);

  const getRulesNext = () => {
    getDetectionRules({ schemeId: currentPlanInfo.id }).then((res: any) => {
      console.log('rules', res);
      setCurrentStep(currentStep + 1);
    });
  };

  const changeBaseInfoSuccess = (data: any) => {
    setCurrentPlanInfo(data);
    if (dialogType === EnumDialogType.edit) {
      getRulesNext();
    } else {
      setCurrentStep(currentStep + 1);
    }
  };

  const onFinish = (values: any) => {
    const { id: archiveTypeId, atCode, atType } = selectAT;
    const params: IAddPlanParams = {
      archiveTypeId,
      atCode,
      atType,
      detectionObjType: currentObjType.id,
      ...values
    };
    if (currentPlanInfo.id) {
      editDetectionPlanFunc({ ...params, id: currentPlanInfo.id }).then((data: any) => {
        changeBaseInfoSuccess(data);
      });
    } else {
      addDetectionPlanFunc(params).then((data: any) => {
        changeBaseInfoSuccess(data);
      });
    }
  };

  return (
    <div className={cs(cx('step-pane'), cx('base-info'))}>
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        preserve={false}
        form={form}
        onFieldsChange={() => {
          setEditFlag(true);
        }}
        onFinish={onFinish}
      >
        <Form.Item label="档案类型">{selectAT.atName}</Form.Item>
        <Form.Item label="检测对象">{currentObjType.name}</Form.Item>
        <Form.Item name="schemeName" label="方案名称" rules={[{ required: true }]}>
          <Input allowClear />
        </Form.Item>
        <Form.Item name="schemeDesc" label="方案说明">
          <Input.TextArea placeholder="请填写检测方案说明" maxLength={2000} showCount />
        </Form.Item>
      </Form>
    </div>
  );
});

export default BaseInfo;
